package com.problem;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 顺序循环打印ABC
 */
public class PrintABC {


    public static void main(String[] args) {

        ReentrantLock lock = new ReentrantLock();
        Condition cA = lock.newCondition();
        Condition cB = lock.newCondition();
        Condition cC = lock.newCondition();


        Thread A = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    lock.lock();
                    System.out.println("A");
                    cB.signal();
                    try {
                        cA.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    lock.unlock();
                }
            }
        });

        Thread B= new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    lock.lock();
                    System.out.println("B");
                    cC.signal();
                    try {
                        cB.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    lock.unlock();
                }
            }
        });

        final Thread C = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    lock.lock();
                    System.out.println("C");
                    cA.signal();
                    try {
                        cC.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    lock.unlock();
                }
            }
        });

        C.start();

        A.start();
        try {
            Thread.sleep(100l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        B.start();
        try {
            Thread.sleep(100l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public static void main1(String[] args) throws InterruptedException {
        Thread A,B,C;

        C = new Thread(new Runnable() {
            @Override
            public void run() {
                    System.out.println("C");
            }
        });

        B= new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("B");
                    try {
                        C.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });


        A = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("A");
                    try {
                        B.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });



        A.start();
        A.join();
        B.start();
        C.start();

    }
}
